/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.da;

import com.stip.server.entities.Activeties;
import com.stip.server.entities.UserAct;
import com.stip.server.entities.UsersMain;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.*;
/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
public class UserActDA {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
@PersistenceContext   
EntityManager em;
public  UserAct findActByID(Integer id){
return  em.find(UserAct.class, id);
}
public UserAct findActByActUid(String uid,Integer actID ){
UserAct userAct;
Query query=em.createQuery("SELECT u FROM UserAct u WHERE u.userId = :userId and u.actId = :actId");
query.setParameter("userId", uid);
query.setParameter("actId", actID);
    try {
        userAct=(UserAct)query.getSingleResult();
        return  userAct;
    } catch (NoResultException e) {
        return  null;
    }
    catch(NonUniqueResultException e){
    return  null;
    }
}
public  Integer createAct(Integer actID,String usersID,Integer statues){
UserAct user=new UserAct();
user.setStatues(statues);
user.setActId(em.find(Activeties.class, actID));
user.setUserId(em.find(UsersMain.class, usersID));
em.persist(user);
return  user.getId();
}
public Boolean editUAct(Integer userActID,Integer actID,String usersID,Integer statues){
UserAct userAct=em.find(UserAct.class, userActID);
    if (userAct!=null) {
        if (actID==null) {
           userAct.setActId(userAct.getActId());
        } else {
            userAct.setActId(em.find(Activeties.class, actID));
        }
        if (usersID==null) {
            userAct.setUserId(userAct.getUserId());
        } else {
            userAct.setUserId(em.find(UsersMain.class, usersID));
        }
        if (statues==null) {
            userAct.setStatues(userAct.getStatues());
        } else {
            userAct.setStatues(statues);
        }
        em.merge(userAct);
        return true;
    } else {
        return  false;
    }
    
}
public Boolean deleteUact(Integer userActID){
UserAct userAct=em.find(UserAct.class, userActID);
    if (userAct!=null) {
        em.remove(userAct);
        return  true;
    } else {
        return  false;
    }
}
}
